package trinnes.oliver.unimaas.smartpentalath;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import trinnes.oliver.unimaas.smartpentalath.control.Game;
import trinnes.oliver.unimaas.smartpentalath.control.HumanPlayer;
import trinnes.oliver.unimaas.smartpentalath.control.Player;
import trinnes.oliver.unimaas.smartpentalath.data.Cell;
import trinnes.oliver.unimaas.smartpentalath.data.Piece;

/**
 *
 * @author Oliver Trinnes
 */
public class HexagonPanel extends JPanel
{

  public static final int SIZE = 40;
  private static final long serialVersionUID = 1L;
  private Cell cell;
  private Polygon hexagon;

  public HexagonPanel(final Game game, final Cell cell, Polygon hexagon)
  {
    super();
    this.cell = cell;
    this.hexagon = hexagon;
    setOpaque(false);
    addMouseListener(new MouseListener()
    {

      public void mouseClicked(MouseEvent e)
      {
      }

      public void mousePressed(MouseEvent e)
      {
        Player current = game.getCurrentPlayer();
        if (current != null && current instanceof HumanPlayer)
            current.setNextMove(cell);
      }

      public void mouseReleased(MouseEvent e)
      {
      }

      public void mouseEntered(MouseEvent e)
      {
      }

      public void mouseExited(MouseEvent e)
      {
      }
    });
  }

  public void setCell(Cell cell)
  {
    this.cell = cell;
    repaint();
  }

  @Override
  public void paint(Graphics g)
  {
    super.paint(g);
    
    g.setColor(Color.yellow);
    g.fillPolygon(hexagon);
    g.setColor(Color.black);
    g.drawPolygon(hexagon);

    if (cell.getPiece().equals(Piece.BLACK))
    {
      g.setColor(Color.black);
      g.fillOval((int) (0.15 * SIZE), (int) (0.15 * SIZE)-1, (int) (0.7 * SIZE), (int) (0.7 * SIZE));
    }
    else if (cell.getPiece().equals(Piece.WHITE))
    {
      g.setColor(Color.white);
      g.fillOval((int) (0.15 * SIZE), (int) (0.15 * SIZE)-1, (int) (0.7 * SIZE), (int) (0.7 * SIZE));
      g.setColor(Color.black);
      g.drawOval((int) (0.15 * SIZE), (int) (0.15 * SIZE)-1, (int) (0.7 * SIZE), (int) (0.7 * SIZE));
    }
  }

  @Override
  public boolean contains(int x, int y)
  {
    return hexagon.contains(x, y);
  }

}
